package com.ansion.basedb.util;

import android.content.Intent;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.ansion.basedb.dao.model.GroupModel;
import com.ansion.basedb.dao.model.UserModel;
import com.ansion.utils.ResourceUtils;

import java.io.Serializable;
import java.util.List;

public class UserBroadcastUtils {
    public static final String kDAOAction_UserTable = "kDAOAction_UserTable";
    public static final String kDAOAction_UserTableBatch = "kDAOAction_UserTableBatch";
    public static final String KEY_USERMODELCHANGEINTENTEXTRA = "fromMessage";
    public static final String kDAOCategory_RowReplace = "kDAOCategory_RowReplace";
    public static final String kDAOCategory_RowRemove = "kDAOCategory_RowRemove";
    public static final String kDAOAction_ContactsTable = "kDAOAction_ContactsTable";
    public static final String kDAOCategory_LoadEnd = "kDAOCategory_LoadEnd";
    public static final String kDAOAction_GroupTable = "kDAOAction_GroupTable";
    public static final String kDAOAction_SyncGroups = "kDAOAction_SyncGroups";
    public static final String ACTION_DEALGROUP_TO_FAV = "action_dealgroup_to_fav";

    public static void sendUserModelChangeIntentFromMessage(String category, UserModel user) {
        Intent intent = new Intent(kDAOAction_UserTable);
        intent.addCategory(category);
        intent.putExtra(KEY_USERMODELCHANGEINTENTEXTRA, true);
        intent.putExtra("user", user);
        LocalBroadcastManager.getInstance(ResourceUtils.getContext())
                .sendBroadcast(intent);
    }

    /**
     * 单个UserModel
     *
     * @param category
     * @param user
     */
    public static void sendUserModelChangeIntent(String category, UserModel user) {
        Intent intent = new Intent(kDAOAction_UserTable);
        intent.addCategory(category);
        intent.putExtra("user", user);
        LocalBroadcastManager.getInstance(ResourceUtils.getContext())
                .sendBroadcast(intent);
    }

    /**
     * 批量UserModel数据改变
     *
     * @param category
     * @param userList
     */
    public static void sendUserModelBatchChangeIntent(String category, List<UserModel> userList) {
        Intent intent = new Intent(kDAOAction_UserTableBatch);
        intent.addCategory(category);
        intent.putExtra("userList", (Serializable) userList);
        LocalBroadcastManager.getInstance(ResourceUtils.getContext())
                .sendBroadcast(intent);
    }

    public static void sendContactChangeIntent(String category) {
        Intent intent = new Intent(kDAOAction_ContactsTable);
        intent.addCategory(category);
        LocalBroadcastManager.getInstance(ResourceUtils.getContext())
                .sendBroadcast(intent);
    }

    /**
     * group改变
     *
     * @param category
     * @param groupModel
     */
    public static void sendGroupChangeIntent(String category, GroupModel groupModel) {

        Intent intent = new Intent(kDAOAction_GroupTable);
        intent.addCategory(category);
        intent.putExtra("group", groupModel);
        LocalBroadcastManager.getInstance(ResourceUtils.getContext())
                .sendBroadcast(intent);
    }

    public static void sendGroupChangeIntent(String category, List<GroupModel> groupList) {

        Intent intent = new Intent(kDAOAction_SyncGroups);
        intent.addCategory(category);
        intent.putExtra("groupList", (Serializable) groupList);
        LocalBroadcastManager.getInstance(ResourceUtils.getContext())
                .sendBroadcast(intent);
    }

    public static void sendDeleteFavGroupIntent() {
        Intent intent = new Intent(ACTION_DEALGROUP_TO_FAV);
        LocalBroadcastManager.getInstance(ResourceUtils.getContext())
                .sendBroadcast(intent);
    }

}
